package com.testapp.bluetoothtest08.Http;




import com.testapp.bluetoothtest08.Constant.Constants;



import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttp {
    public String httpGet(String url){
        String result=null;
        try {
            OkHttpClient client = new OkHttpClient();
            Request request =new Request.Builder()
                    .addHeader("token", Constants.token)
                    .url(Constants.serverURL+url)
                    .get()
                    .build();
            Response response =client.newCall(request).execute();
            result=response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public String httpGetWithNoToken(String url){
        String result=null;
        try {
            OkHttpClient client = new OkHttpClient();
            Request request =new Request.Builder()
//                    .addHeader("token", Constants.token)
                    .url(Constants.serverURL+url)
                    .get()
                    .build();
            Response response =client.newCall(request).execute();
            result=response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public String httpPost(String url, String text){
        String result=null;
        try {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(Constants.serverURL+url)
                    .post(RequestBody.create(MediaType.parse("application/json"),text))
                    .build();
            Response response = client.newCall(request).execute();
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
    public String httpPostWithToken(String url, String text){
        String result=null;
        try {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .addHeader("token",Constants.token)
                    .url(Constants.serverURL+url)
                    .post(RequestBody.create(MediaType.parse("application/json"),text))
                    .build();
            Response response = client.newCall(request).execute();
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
